{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b77ccaf",
   "metadata": {},
   "outputs": [],
   "source": [
    "SCOPETYPE = 'OPENADC'\n",
    "PLATFORM = 'CW308_STM32F4'\n",
    "CRYPTO_TARGET = 'TINYAES128C'\n",
    "SS_VER = 'SS_VER_2_1'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "aa6d0293",
   "metadata": {},
   "outputs": [],
   "source": [
    "SAVE_REF = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e4e29848",
   "metadata": {},
   "outputs": [],
   "source": [
    "passed = True\n",
    "ERR_MSG=\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ff630d5a",
   "metadata": {},
   "outputs": [],
   "source": [
    "%%bash -s \"$PLATFORM\" \"$CRYPTO_TARGET\" \"$SS_VER\"\n",
    "cd ../../firmware/mcu/simpleserial-aes\n",
    "make PLATFORM=$1 CRYPTO_TARGET=$2 SS_VER=$3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ed5f882d",
   "metadata": {},
   "outputs": [],
   "source": [
    "%run \"../Setup_Scripts/Setup_Generic.ipynb\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d3d1367",
   "metadata": {},
   "outputs": [],
   "source": [
    "fw_path = '../../firmware/mcu/simpleserial-aes/simpleserial-aes-{}.hex'.format(PLATFORM)\n",
    "cw.program_target(scope, prog, fw_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fc92e6fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "scope.clock.adc_src = \"clkgen_x1\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "211e8ac2",
   "metadata": {},
   "outputs": [],
   "source": [
    "scope.adc.stream_mode = True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5c0a9205",
   "metadata": {},
   "outputs": [],
   "source": [
    "scope.adc.basic_mode = \"rising_edge\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "592ecbc9",
   "metadata": {},
   "outputs": [],
   "source": [
    "scope.adc.samples=1000000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "756385c9",
   "metadata": {},
   "outputs": [],
   "source": [
    "ktp = cw.ktp.Basic()\n",
    "key, text = ktp.next()\n",
    "ret = cw.capture_trace(scope, target, text, key)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d57de24f",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert len(ret.wave) == 1000000, \"Incorrect wave length {}\".format(ret.wave)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "128a08af",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "if (SAVE_REF):\n",
    "    np.save(\"pro_stream.npy\", ret.wave)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ba345e8e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "ref_wave = np.load(\"pro_stream.npy\", allow_pickle=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8c0ea61f",
   "metadata": {},
   "outputs": [],
   "source": [
    "cw.plot(ret.wave)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a589ce6",
   "metadata": {},
   "outputs": [],
   "source": [
    "sad = np.sum(np.abs(ref_wave - ret.wave))\n",
    "#assert(sad < 5000), \"Bad stream (must be <5000), SAD = {}\".format(sad)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d3e3cb2b",
   "metadata": {},
   "outputs": [],
   "source": [
    "SAD_point = int(120)\n",
    "scope.adc.stream_mode = False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1fd3ffaa",
   "metadata": {},
   "outputs": [],
   "source": [
    "scope.SAD.reference = ret.wave[SAD_point:SAD_point+128]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f34ede7",
   "metadata": {},
   "outputs": [],
   "source": [
    "scope.SAD.threshold = 2000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eaac8b35",
   "metadata": {},
   "outputs": [],
   "source": [
    "scope.SAD.start()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d027714d",
   "metadata": {},
   "outputs": [],
   "source": [
    "scope.trigger.module = \"SAD\"\n",
    "scope.adc.basic_mode = \"rising_edge\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "189b81e8",
   "metadata": {},
   "outputs": [],
   "source": [
    "scope.adc.samples=5000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "308c8392",
   "metadata": {},
   "outputs": [],
   "source": [
    "from tqdm import tnrange\n",
    "project = cw.create_project(\"projects/Tutorial_B5\", overwrite = True)\n",
    "for i in tnrange(100, desc='Capturing traces'):\n",
    "    key, text = ktp.next()  # manual creation of a key, text pair can be substituted here\n",
    "    trace = cw.capture_trace(scope, target, text, key)\n",
    "    if trace is None:\n",
    "        continue\n",
    "    project.traces.append(trace)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fcc01095",
   "metadata": {},
   "outputs": [],
   "source": [
    "cw.plot(project.waves[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ca102a87",
   "metadata": {},
   "outputs": [],
   "source": [
    "import chipwhisperer.analyzer as cwa\n",
    "leak_model = cwa.leakage_models.sbox_output\n",
    "attack = cwa.cpa(project, leak_model)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "614ba405",
   "metadata": {},
   "outputs": [],
   "source": [
    "results = attack.run(None, 100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f0a0730a",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(results)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "76f05cba",
   "metadata": {},
   "outputs": [],
   "source": [
    "print(results.find_key(), \"\\n\", list(project.keys[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d557bfa2",
   "metadata": {},
   "outputs": [],
   "source": [
    "assert results.find_key() == list(project.keys[0]), \"Failed to break key!\\nGot: {}\\nExp: {}\".format(results.find_key(), list(project.keys[0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d306e42a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
